package com.runa.monitor.platinterface.netservice.equipment.dtu.point;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.Point;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.monitor.platinterface.mqtt.vo.FourGValveVO;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.persistence.DateUtil;
import com.runa.protocol.dto.ResponseContent;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.enums.ModbusDataType;
import com.runa.protocol.util.ByteUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 2024/9/11 9:26
 * zhangYB
 */
@Slf4j
public class FourGValvePoints extends ModbusPoints {

    public FourGValvePoints() {
        super(60);
        init();
    }

    private void init() {
        initIds();
        initPoints();
    }


    public void initIds() {
    }

    public void initPoints() {
        points = new ArrayList<>();
        points.add(new Point(400001, "remoteSet", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));
        points.add(new Point(400003, "actOpen", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));
        points.add(new Point(400008, "softVersion", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));

        points.add(new Point(400010, "supplyTemp", ModbusDataType.Modbus_Float.getValue(), "CDAB", 0));
        points.add(new Point(400012, "returnTemp", ModbusDataType.Modbus_Float.getValue(), "CDAB", 0));

        points.add(new Point(400032, "status", ModbusDataType.Modbus_UTF8.getValue(), "AB", 0));
        points.add(new Point(400033, "LR", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));
        points.add(new Point(400034, "model", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));

//        points.add(new Point(400095, "openTime", ModbusDataType.Modbus_UInt32.getValue(), "DCBA", 0));

        points.add(new Point(400106, "batteryVol", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));

        points.add(new Point(400125, "year", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));
        points.add(new Point(400126, "month", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));
        points.add(new Point(400127, "day", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));
        points.add(new Point(400128, "hour", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));
        points.add(new Point(400129, "minute", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));
        points.add(new Point(400130, "second", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));

        points.add(new Point(400270, "rsrp", ModbusDataType.Modbus_Int16.getValue(), "AB", 0));
        points.add(new Point(400303, "signal", ModbusDataType.Modbus_UInt16.getValue(), "AB", 0));

        ids = points.stream().map(Point::getId).collect(Collectors.toList());
    }

    public void handle(ResponseContent responseContent) {
        responseContent.put("valveDate", DateUtil.noLineFormatToDate("20"+responseContent.get("year")
                +responseContent.get("month")+responseContent.get("day")+responseContent.get("hour")+responseContent.get("minute")+responseContent.get("second")));
        responseContent.put("readTime", new Date());

        Object status = responseContent.get("status");
        if (null != status) {
            responseContent.put("powerType", (Integer.parseInt(String.valueOf(status), 16) >> 11) & 1);
            responseContent.put("faultCode", FourGValveVO.faultStr(String.valueOf(status)));
        }

        Object batteryVol = responseContent.get("batteryVol");
        if (null != batteryVol) {
            responseContent.put("batteryVol", Integer.valueOf(String.valueOf(batteryVol)) / 1000);
        }

        responseContent.put("LR", responseContent.get("LR") == null ? null : String.valueOf(responseContent.get("LR")).equalsIgnoreCase("0") ? ValveVO.LREnum.本地 : ValveVO.LREnum.远程);
        Object model = responseContent.get("model");
        if (null != model) {
            responseContent.put("model", ValveVO.geneModel(Integer.valueOf(String.valueOf(model))));
        }
        log.debug("处理后点位: {}", JSON.toJSONString(responseContent));
    }

    private Map<String, Point> pointMap;

    public Point getByCode(String code) {
        if (null == pointMap) {
            pointMap = points.stream().collect(Collectors.toMap(Point::getCode, Function.identity(), (p1, p2) -> p1));
        }
        return pointMap.get(code);
    }
}
